#include "addfriendwidget.h"
#include "ui_addfriendwidget.h"
AddFriendWidget::AddFriendWidget(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AddFriendWidget)
{
    ui->setupUi(this);
    this->setWindowTitle("添加好友");
    this->setAttribute(Qt::WA_DeleteOnClose);
    ui->listWidget->setViewMode(QListView::IconMode);
    ui->listWidget->setMovement(QListView::Static);
    useroc=FriendListLib::getInstance()->getUserOC();
    connect(CustomHandler::getInstance(),&CustomHandler::s_recvFriendInfo,this,&AddFriendWidget::loadFriendToUI);
}


AddFriendWidget::~AddFriendWidget()
{
    delete ui;
}

void AddFriendWidget::addFriendToList(const QString& friendoc,const QString& friendname)
{
    QListWidgetItem* item=new QListWidgetItem("");
    ui->listWidget->addItem(item);
    AddFriendBox* box=new AddFriendBox;
    box->setFriendName(useroc,friendoc,friendname);
    ui->listWidget->setItemWidget(item,box);
    item->setSizeHint(box->getSize());
    connect(box,&AddFriendBox::btn_clicked,[=](const QString friendname){
        ThreadManger::getInstance()->sendFriendApply(friendoc);
        ThreadManger::getInstance()->addFriendApply(friendoc,friendname,EnumLib::ApplyStatus::TARGETUNDERREVIEW);
        FriendApplyLib::getInstance()->addApply(friendoc,friendname,EnumLib::ApplyStatus::TARGETUNDERREVIEW);
    });
    resultfriend=box;
}


void AddFriendWidget::on_btn_search_clicked()
{
    if(ui->lineEdit_search->text().isEmpty())return;
    this->friendoc=ui->lineEdit_search->text();
    ThreadManger::getInstance()->sendSearchFriend(friendoc);
    ui->listWidget->clear();
    resultfriend=nullptr;
}

void AddFriendWidget::loadFriendToUI(const QString& friendname)
{
    addFriendToList(friendoc,friendname);
    resultfriend->setExists(ContactLib::getInstance()->contain(friendoc)||FriendApplyLib::getInstance()->friendIsUnderrview(friendoc));
}
